package cn.edu.scau.cmi.maikejian.comprehension.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import cn.edu.scau.cmi.maikejian.comprehension.domain.Student;
import cn.edu.scau.cmi.maikejian.comprehension.domain.Teacher;


public class TeacherDAO {
	static Connection conn;
	public TeacherDAO(Connection conn) {
		this.conn=conn;
	}
	
	public static int newTeacher(Teacher teacher) {

	    int i = 0;
	    String sql = "insert into teacher (id,name) values(?,?)";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        pstmt.setString(2, teacher.getName());
	        pstmt.setLong(1, teacher.getId());
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        //conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
	
	public static int update_name(Teacher teacher) {

	    int i = 0;
	    String sql = "update teacher set name='" + teacher.getName() + "' where id='" + teacher.getId() + "'";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        i = pstmt.executeUpdate();
	        pstmt.close();
	        //conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
	
	public static Teacher getTeacher(long id) {
		Teacher tea=null;
		String sql = "select * from teacher where id='"+id+"'";
		PreparedStatement pstmt;
		try {
			pstmt = (PreparedStatement)conn.prepareStatement(sql);
			ResultSet rs = pstmt.executeQuery();
			if(rs.next()) {
				int col = rs.getMetaData().getColumnCount();
		        long tea_id = rs.getLong(1);
		        String name = rs.getString(2);
		        tea = new Teacher(tea_id,name);
			}
	        
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return tea;
	}
	
	public static Integer listAllTeachers() {

	    String sql = "select * from teacher";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement)conn.prepareStatement(sql);
	        ResultSet rs = pstmt.executeQuery();
	        int col = rs.getMetaData().getColumnCount();
	        System.out.println("============================");
	        while (rs.next()) {
	            for (int i = 1; i <= col; i++) {
	                System.out.print(rs.getString(i) + "\t");
	                if ((i == 2) && (rs.getString(i).length() < 8)) {
	                    System.out.print("\t");
	                }
	             }
	            System.out.println("");
	        }
	            System.out.println("============================");
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return null;
	}
	
	public static int delete(long id) {

	    int i = 0;
	    String sql = "delete from teacher where id='" + id + "'";
	    PreparedStatement pstmt;
	    try {
	        pstmt = (PreparedStatement) conn.prepareStatement(sql);
	        i = pstmt.executeUpdate();
	        System.out.println("resutl: " + i);
	        pstmt.close();
	        //conn.close();
	    } catch (SQLException e) {
	        e.printStackTrace();
	    }
	    return i;
	}
}